package com.gurid.service.cms.controller.api;

import com.gurid.common.base.result.R;
import com.gurid.service.cms.entity.Ad;
import com.gurid.service.cms.service.AdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "ApiAdController", description = "广告推荐展示")
@RestController
//@CrossOrigin
@RequestMapping("/api/cms/ad")
@Slf4j
public class ApiAdController {

    @Autowired
    private AdService adService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Operation(description = "根据推荐位id显示广告推荐")
    @GetMapping("list/{adTypeId}")
    public R listByAdTypeId(
            @Parameter(description = "推荐位id", required = true)
            @PathVariable String adTypeId) {

        List<Ad> ads = adService.selectByAdTypeId(adTypeId);
        return R.ok().data("items", ads);
    }

    @Operation(description = "将广告信息缓存到redis中")
    @PostMapping("save-test")
    public R saveAd(@RequestBody Ad ad){
        redisTemplate.opsForValue().set("index::myad", ad);
        return R.ok();
    }

    @GetMapping("get-test/{key}")
    public R getAd(@PathVariable String key){
        Ad ad = (Ad) redisTemplate.opsForValue().get(key);//操作字符串
        // redisTemplate.opsForHash()//操作hash
        // redisTemplate.opsForList()//操作List
        // redisTemplate.opsForSet()//操作set
        // redisTemplate.opsForZSet()//操作有序Set
        return R.ok().data("ad", ad);
    }

    @DeleteMapping("remove-test/{key}")
    public R removeAd(@PathVariable String key){
        Boolean delete = redisTemplate.delete(key);
        System.out.println(delete);
        Boolean b = redisTemplate.hasKey(key);
        System.out.println(b);
        return R.ok();
    }
}
